package com.model.mapparser;

import java.util.Date;

import com.core.RequestParams;
import com.model.project.ProjectItem;
import com.model.project.attributes.TaskPriority;
import com.model.project.attributes.TaskResolution;
import com.model.project.attributes.TaskStatus;
import com.model.project.attributes.TaskType;
import com.model.user.User;
import com.servicelayer.project.attributes.TaskPriorityServiceLayer;
import com.servicelayer.project.attributes.TaskResolutionServiceLayer;
import com.servicelayer.project.attributes.TaskStatusServiceLayer;
import com.servicelayer.project.attributes.TaskTypeServiceLayer;
import com.servicelayer.user.UserServiceLayer;


public class ProjectItemMapParser {

    public static ProjectItem fromMap ( RequestParams requestParams ) {

        if ( requestParams == null ) {
            return null;
        }


        Integer projectTypeId = requestParams.integerAtIndexForKey(0, "projectType");

        Integer projectPriorityId = requestParams.integerAtIndexForKey(0, "projectPriority");

        Integer projectStatusId = requestParams.integerAtIndexForKey(0, "projectStatus");

        Integer projectResolutionId = requestParams.integerAtIndexForKey(0, "projectResolution");

        String name = requestParams.stringAtIndexForKey(0, "name");

        String description = requestParams.stringAtIndexForKey(0, "description");

        Integer reporterId = requestParams.integerAtIndexForKey(0, "reporter");

        Integer assigneeId = requestParams.integerAtIndexForKey(0, "assignee");

        Date createdDate = requestParams.dateAtIndexForKeyWithFormat(0, "createDate", "dd.MM.yyyy");

        Date releaseDate = requestParams.dateAtIndexForKeyWithFormat(0, "releaseDate", "dd.MM.yyyy");


        TaskTypeServiceLayer taskTypeServiceLayer = new TaskTypeServiceLayer();
        TaskType projectType = (TaskType) taskTypeServiceLayer.objectByUNID(projectTypeId, null, null);

        TaskPriorityServiceLayer taskPriorityServiceLayer = new TaskPriorityServiceLayer();
        TaskPriority projectPriority = (TaskPriority) taskPriorityServiceLayer.objectByUNID(projectPriorityId, null, null);

        TaskStatusServiceLayer taskStatusServiceLayer = new TaskStatusServiceLayer();
        TaskStatus projectStatus = (TaskStatus) taskStatusServiceLayer.objectByUNID(projectStatusId, null, null);

        TaskResolutionServiceLayer taskResolutionServiceLayer = new TaskResolutionServiceLayer();
        TaskResolution projectResolution = (TaskResolution) taskResolutionServiceLayer.objectByUNID(projectResolutionId, null, null);

        UserServiceLayer userServiceLayer = new UserServiceLayer();
        User taskAssignee = (User) userServiceLayer.objectByUNID(assigneeId, null, null);
        User taskReporter = (User) userServiceLayer.objectByUNID(reporterId, null, null);

        ProjectItem template = null;
        template = new ProjectItem();
        template.setName(name);
        template.setDescription(description);
        template.setTaskType(projectType);
        template.setTaskPriority(projectPriority);
        template.setTaskStatus(projectStatus);
        template.setTaskResolution(projectResolution);
        template.setAssignee(taskAssignee);
        template.setReporter(taskReporter);
        template.setCreateDate(createdDate);
        template.setReleaseDate(releaseDate);

        return template;

    }

}
